package com.xu.springboot04.servlet;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;

/**
 * 定制化的常见方式：
 *
 * 1.@Bean 替换、增加容器中默认组件、视图解析器
 * 2.修改配置文件
 * 3.xxxxxCustomer
 * 4.编写自定义的配置类 xxxConfiguration
 * 5.web 应用实现 WebMvcConfigurer 即可定制化 web 功能
 * 6.@EnableWebMvc + WebMvcConfigurer --> @Bean 即可全面接管 SpringMVC，所有的规则全部自己重新配置，实现定制和扩展功能
 *
 * 原理分析套路
 *
 * 场景 starter -> xxxxAutoConfiguration -> 导入 xxx 组件 -> 绑定 xxxProperties
 */

@Configuration
public class MyServletConfig {

    // 注册原生 Servlet 到容器中
    @Bean
    public ServletRegistrationBean<MyServlet> myServlet(){
        return new ServletRegistrationBean<>(new MyServlet(), "/servlet", "/servlet02");
    }

    @Bean
    public FilterRegistrationBean<MyFilter> myFilter(){
        MyFilter myFilter = new MyFilter();
        FilterRegistrationBean<MyFilter> filterRegistrationBean = new FilterRegistrationBean<>(myFilter);
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/servlet", "/css/*"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<MyServletContextListener> myListener(){
        MyServletContextListener servletContextListener = new MyServletContextListener();
        return new ServletListenerRegistrationBean<>(servletContextListener);
    }

}
